﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Example10.aspx.cs" Inherits="Chapter28.Example10" %>

<!DOCTYPE html>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>代码清单 28-10 创建和删除元素</title>
    <meta name="author" content="Adam Freeman" />
    <meta name="description" content="A simple example" />
    <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
    <style>
        table {
            border: solid thin black;
            border-collapse: collapse;
            margin: 10px;
        }

        td {
            padding: 4px 5px;
        }
    </style>
</head>
<body>
    <table border="1">
        <thead>
            <tr>
                <th>Name</th>
                <th>Color</th>
            </tr>
        </thead>
        <tbody id="fruitsBody">
            <tr>
                <td>Banana</td>
                <td>Yellow</td>
            </tr>
            <tr>
                <td>Apple</td>
                <td>Red/Green</td>
            </tr>
        </tbody>
    </table>

    <button id="add">Add Element</button>
    <button id="remove">Remove Element</button>

    <script>
        var talbeBody = document.getElementById("fruitsBody");

        document.getElementById("add").onclick = function () {
            var row = talbeBody.appendChild(document.createElement("tr"));
            row.setAttribute("id", "newrow");
            row.appendChild(document.createElement("td")).appendChild(document.createTextNode("Plum"));
            row.appendChild(document.createElement("td")).appendChild(document.createTextNode("Purple"));
        };

        document.getElementById("remove").onclick = function () {
            var row = document.getElementById("newrow");
            row.parentNode.removeChild(row);
        };
    </script>
</body>
</html>
